package com.aspirecn.order.service.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.aspirecn.order.client.UserServiceClient;
import com.aspirecn.order.client.UserServiceNoDiscoClient;
import com.aspirecn.order.common.RestApiConstants;
import com.aspirecn.order.domain.RequestHead;
import com.aspirecn.order.domain.RequestMsg;
import com.aspirecn.order.domain.ResponseMsg;
import com.aspirecn.order.domain.ResultBean;
import com.aspirecn.order.domain.vo.QueryOrderInfoByIdOutVo;
import com.aspirecn.order.domain.vo.QueryUserInfoByUserNameInVo;
import com.aspirecn.order.domain.vo.QueryUserInfoByUserNameOutVo;
import com.aspirecn.order.repository.OrderRepository;
import com.aspirecn.order.service.OrderService;

/**
 * @author chenyuanli
 */
@Service
//@PropertySource("redis")
//@PropertySource(value = {"${config.path}"}, encoding="utf-8")
public class OrderServiceImpl implements OrderService {
	private static final Logger LOG = LoggerFactory.getLogger(OrderServiceImpl.class);

	@Autowired
	private OrderRepository repository;

	@Autowired
	private UserServiceClient userServiceClient;
	
	@Autowired
	private UserServiceNoDiscoClient userServiceNoDiscoClient;
	
	//redis后缀的配置文件
	@Value("${email}")
	private String emailRedis;
	
	//mysql后缀的配置文件
	@Value("${username}")
	private String usernameMysql;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public ResultBean<QueryOrderInfoByIdOutVo> queryOrderInfoById(String id) {
		ResultBean<QueryOrderInfoByIdOutVo> result = new ResultBean<QueryOrderInfoByIdOutVo>();
		
		RequestMsg<QueryUserInfoByUserNameInVo> request = new RequestMsg<QueryUserInfoByUserNameInVo>();
		RequestHead head = new RequestHead();
		request.setHead(head);
		
		head.setRequestId(java.util.UUID.randomUUID().toString());
		
		LOG.info("---------------------- emailRedis:{}", emailRedis);
		LOG.info("---------------------- usernameMysql:{}", usernameMysql);
		
		QueryOrderInfoByIdOutVo orderInfo = repository.findOrderInfoByid(id);
		if (orderInfo == null) {
			result.setCode(ResultBean.FAIL);
			result.setMsg("can not find order id " + id);	
			return result;
		}
		
		QueryUserInfoByUserNameInVo vo = new QueryUserInfoByUserNameInVo();
		vo.setUserName(orderInfo.getUsername());
		request.setData(vo);
		
		ResponseMsg<QueryUserInfoByUserNameOutVo> response = userServiceClient.queryUserInfoByUserName(request);
		if (response != null && RestApiConstants.RESPCODE_SUCCESS.equals(response.getHead().getRespCode())) {		
			QueryOrderInfoByIdOutVo outVo = new QueryOrderInfoByIdOutVo();
			outVo.setId(orderInfo.getId());
			outVo.setEmail(response.getData().getEmail());
			outVo.setStatus(orderInfo.getStatus());
			outVo.setUsername(vo.getUserName());
			
			result.setData(outVo);
			result.setCode(ResultBean.SUCCESS);
			result.setMsg("SUCCESS");	
		} else {
			result.setCode(ResultBean.FAIL);
			result.setMsg("user service exception");	
		}

		return result;
	}
	
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public ResultBean<QueryOrderInfoByIdOutVo> queryOrderInfoByIdNoDisco(String id) {
		ResultBean<QueryOrderInfoByIdOutVo> result = new ResultBean<QueryOrderInfoByIdOutVo>();
		
		RequestMsg<QueryUserInfoByUserNameInVo> request = new RequestMsg<QueryUserInfoByUserNameInVo>();
		RequestHead head = new RequestHead();
		request.setHead(head);
		
		head.setRequestId(java.util.UUID.randomUUID().toString());
		
		QueryOrderInfoByIdOutVo orderInfo = repository.findOrderInfoByid(id);
		if (orderInfo == null) {
			result.setCode(ResultBean.FAIL);
			result.setMsg("can not find order id " + id);	
			return result;
		}
		
		QueryUserInfoByUserNameInVo vo = new QueryUserInfoByUserNameInVo();
		vo.setUserName(orderInfo.getUsername());
		request.setData(vo);
		
		ResponseMsg<QueryUserInfoByUserNameOutVo> response = userServiceNoDiscoClient.queryUserInfoByUserName(request);
		if (response != null && RestApiConstants.RESPCODE_SUCCESS.equals(response.getHead().getRespCode())) {		
			QueryOrderInfoByIdOutVo outVo = new QueryOrderInfoByIdOutVo();
			outVo.setId(orderInfo.getId());
			outVo.setEmail(response.getData().getEmail());
			outVo.setStatus(orderInfo.getStatus());
			outVo.setUsername(vo.getUserName());
			
			result.setData(outVo);
			result.setCode(ResultBean.SUCCESS);
			result.setMsg("SUCCESS");	
		} else {
			result.setCode(ResultBean.FAIL);
			result.setMsg("user service exception");	
		}

		return result;
	}

}
